<?php

require_once 'classes/LightPressPlugin.php';

class NestedPages extends LightPressPlugin {
    
    var $constructor_args = array(
        'title'             =>'Heading for first-level childless pages.',
        'expanded'          =>'Expand all pages. If false only the current branch will be expanded.',
        'only_leafs_active' =>'Link only leaf pages.');
    var $default_context = LP_CONTEXT_ALL;
    var $description = 'Display  list of nested pages';
    
    var $active = true;
    var $hooks = array('sidebar');
    
    var $title = 'Content';
    var $expanded = true;
    var $only_leafs_active = true;
    
    function NestedPages(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$post) {
        // TODO: run current code for the 'render' hook and add code for the
        //       'parse_post' hook that sets the post content to a list
        //       of child pages if it's empty
        $tpl =& $this->_frontend->tpl;
        // easier to do this without a template, can always convert it later
        $pages =& $this->_frontend->getPages();
        
        $out = '';
        $this->_recurse($pages, $out, 0, true);
        $tpl->setVar('PLUGIN_NESTEDPAGES', $out);
    }
    
    function _recurse(&$tree, &$out, $level, $root=false) {
        // TODO: track parent/ancestor names when recursing so as to use the faster permalink algorithm
        if ($root) {
            $sorted = array();
            $childless = array();
            foreach ($tree as $id=>$page) {
                if (!$page['root_page'])
                    continue;
                if (count($page['children']) > 0)
                    $sorted[$page['post_title']] =& $tree[$id];
                else
                    $childless[$page['post_title']] =& $tree[$id];
            }
            ksort($sorted);
            if (count($childless) > 0) {
                ksort($childless);
                $out .= '<h2>' . $this->title . '</h2><ul>';
                $this->_recurse($childless, $out, 1);
                $out .= '</ul>';
            }
        } else {
            ksort($tree);
            $sorted =& $tree;
        }
        
        $url = $this->_frontend->options['url'];
        
        foreach ($sorted as $title=>$page) {
            if (count($page['children']) == 0) {
                // leaf
                $out .= sprintf(
                    '<li><a href="%s/%s">%s</a></li>',
                    $url,
                    $this->_frontend->getPagePermalink($page['id'], $page['post_name']),
                    $page['post_title']);
            } else {
                if ($this->only_leafs_active) {
                    if ($level == 0) {
                        $out .= '<li><h2>' . $page['post_title'] . '</h2><ul>';
                    } else {
                        $out .= '<li>' . $page['post_title'] . '<ul>';
                    }
                } else {
                    if ($level == 0) {
                        $out .= sprintf(
                            '<li><h2><a href="%s/%s">%s</a></h2><ul>',
                            $url,
                            $this->_frontend->getPagePermalink($page['id'], $page['post_name']),
                            $page['post_title']);
                    } else {
                        $out .= sprintf(
                            '<li><a href="%s/%s">%s</a><ul>',
                            $url,
                            $this->_frontend->getPagePermalink($page['id'], $page['post_name']),
                            $page['post_title']);
                    }
                }
                if ($this->expanded || in_array($page['post_name'], $this->_frontend->context['hierarchy']))
                    $this->_recurse($page['children'], $out, $level + 1);
                $out .= "</ul></li>";
            }
                
        }
    }

}

?>